import Cookies from "js-cookie";
import { useLocalStorage } from '@vueuse/core';

const sessionCache = {
  setItem(key: string, value: string) {
    if (typeof sessionStorage !== 'undefined' && key && value) {
      sessionStorage.setItem(key, value);
    }
  },
  getItem(key: string): string | null {
    if (typeof sessionStorage !== 'undefined' && key) {
      return sessionStorage.getItem(key);
    }
    return null;
  },
  setJSON(key: string, jsonValue: any) {
    if (jsonValue != null) {
      this.setItem(key, JSON.stringify(jsonValue));
    }
  },
  getJSON(key: string): any {
    const value = this.getItem(key);
    if (value != null) {
      return JSON.parse(value);
    }
    return null;
  },
  removeItem(key: string) {
    if (typeof sessionStorage !== 'undefined') {
      sessionStorage.removeItem(key);
    }
  }
}
const localCache = {
  setItem(key: string, value: any) {
    const storage = useLocalStorage(key, null)
    storage.value = value
  },
  getItem(key: string) {
    return useLocalStorage(key, null).value
  },
  setJSON(key: string, value: any) {
    this.setItem(key, JSON.stringify(value))
  },
  getJSON(key: string) {
    const value = this.getItem(key)
    return value ? JSON.parse(value) : null
  },
  removeItem(key: string) {
    const storage = useLocalStorage(key, null)
    storage.value = null
  }
}

export { sessionCache, localCache, Cookies }
